// Plugins
import Components from "unplugin-vue-components/vite";
import Vue from "@vitejs/plugin-vue";
import Vuetify, { transformAssetUrls } from "vite-plugin-vuetify";
import ViteFonts from "unplugin-fonts/vite";
import VueRouter from "unplugin-vue-router/vite";
import vueSetupExtend from "vite-plugin-vue-setup-extend";
import { createHtmlPlugin } from "vite-plugin-html";
// Utilities
import { defineConfig } from "vite";
import { fileURLToPath, URL } from "node:url";

// https://vitejs.dev/config/
console.log(process.env.NODE_ENV);
const isProd = process.env.NODE_ENV === "production";
export default defineConfig({
  plugins: [
    VueRouter(),
    Vue({
      template: { transformAssetUrls },
    }),
    vueSetupExtend(),
    // https://github.com/vuetifyjs/vuetify-loader/tree/master/packages/vite-plugin#readme
    Vuetify({
      autoImport: true,
      styles: {
        configFile: "src/styles/settings.scss",
      },
    }),
    Components(),
    createHtmlPlugin({
      minify: false,
      template: "index.html",
      inject: {
        data: {
          injectScript: isProd
            ? ` <script>
    function replaceHttpsHostname() {
      const protocol = location.protocol;
      const isHttp = protocol === "http:";
      if (isHttp) {
        location.href = "https:" + location.href.replace(protocol, "");
      }
    }
    replaceHttpsHostname();
  </script>`
            : "",
        },
      },
    }),
  ],
  build: {
    outDir: "dist",
  },
  define: { "process.env": {} },
  resolve: {
    alias: {
      "@": fileURLToPath(new URL("../src", import.meta.url)),
    },
    extensions: [".js", ".json", ".jsx", ".mjs", ".ts", ".tsx", ".vue"],
  },

  server: {
    port: 8080,
    host: true,
  },
});
